package cn.maihe.elg.operation.centers.follow.handler.service;

import cn.maihe.elg.operation.centers.follow.channel.service.GuarantorRevokeNoticeChannelService;
import cn.maihe.elg.operation.centers.follow.enums.FollowRespCode;
import cn.maihe.elg.operation.centers.follow.exception.FollowException;
import cn.maihe.elg.operation.centers.follow.handler.FollowBaseResp;
import cn.maihe.elg.operation.centers.follow.handler.FollowCenterTradeService;
import cn.maihe.elg.operation.centers.follow.handler.dto.GuaranteeGiveUpReqDTO;
import cn.maihe.elg.operation.centers.follow.handler.dto.bo.GuaranteeGiveUpBO;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.dto.BaseResp;
import cn.maihe.elg.operation.model.enums.*;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.RevokeOrderInfoService;
import cn.maihe.elg.operation.supports.system.ElgResourceService;
import cn.maihe.elg.operation.utils.IdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * @Description
 * @Author caixh
 * @Date 2021/9/17.
 */
@CenterTradeService(
        provider = CenterProviderEnum.FOLLOW,
        centerNo = CenterNoEnum.SANMING,
        tradeType = TradeType.guarantee_give_up,
        reqClass = GuaranteeGiveUpReqDTO.class,
        respClass = FollowBaseResp.class
)
@Slf4j
public class FollowGuaranteeGiveUpService extends FollowCenterTradeService<GuaranteeGiveUpReqDTO, BaseResp> {

    @Resource
    private ElgResourceService elgResourceService;
    @Resource
    private RevokeOrderInfoService revokeOrderInfoService;
    @Resource
    private GuaranteeService guaranteeService;
    @Resource
    private ProjectInfoService projectInfoService;
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private GuarantorRevokeNoticeChannelService revokeNoticeChannelService;

    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    public BaseResp processService(CenterTradeContext<GuaranteeGiveUpReqDTO, BaseResp> context) {
        GuaranteeGiveUpReqDTO reqBody = context.getReq();
        GuaranteeGiveUpBO data = reqBody.getData();
        Assert.notNull(data, () -> "业务数据不能为空");

//       GuaranteeDO guaranteeDO = guaranteeService.getGuarantee(context.getCenterId(), context.getOrgId(), data.getOrdersn(), null);
//        Assert.notNull(guaranteeDO, () -> "保函不存在");
//        acceptOrderInfoService.getByCenterOrderId(context.getCenterId(), context.getOrgId(), data.getOrdersn());
        AcceptOrderInfoDO acceptOrderInfoDO = acceptOrderInfoService.getByAcceptOrderNo(data.getOrdersn());
        Assert.notNull(acceptOrderInfoDO, () -> "保函申请订单未找到");
//        if (acceptOrderInfoDO.getStatus() != AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
//            throw FollowException.of(FollowRespCode.FAILURE, "当前保函订单状态不能放弃申请", null);
//        }

        if (acceptOrderInfoDO.getStatus() == AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
            throw FollowException.of(FollowRespCode.FAILURE,"已出函，不能放弃申请",null);
        }
        if (acceptOrderInfoDO.getStatus() == AcceptOrderStatus.FINISH.getStatus()) {
            throw FollowException.of(FollowRespCode.FAILURE,"保函已完成，不能放弃申请",null);
        }

        AcceptOrderInfoDO updateOrderInfo = AcceptOrderInfoDO.builder().id(acceptOrderInfoDO.getId())
                .status(AcceptOrderStatus.CANCEL.getStatus()).remark("放弃申请关闭订单").build();
        acceptOrderInfoService.updateById(updateOrderInfo);

        ProjectInfoDO projectInfo = projectInfoService.selectById(acceptOrderInfoDO.getProjectId());
        RevokeApplayOrderInfoDO revokeApplayOrderInfoDO = RevokeApplayOrderInfoDO.builder()
                .id(IdGenerate.getId())
                .centerId(projectInfo.getCenterId())
                .orgId(context.getOrgId())
                .centerOrderId(data.getOrdersn())
                .revokeNo(data.getOrdersn())
                .projectCode(projectInfo.getProjectCode()).projectName(acceptOrderInfoDO.getProjectName())
                .sectionCode(projectInfo.getSectionCode()).sectionName(acceptOrderInfoDO.getSectionName())
                .tenderer(projectInfo.getTenderer()).status(1)
                .build();
        acceptOrderInfoService.revokeOrder(acceptOrderInfoDO, revokeApplayOrderInfoDO);

//        asyncRevokeNotice(guaranteeDO);

        return null;
    }
}
